package com.example.demo.Controller;

import com.example.demo.Services.impl.GsServicesImpl;
import com.example.demo.Services.impl.TjServicesImpl;
import com.example.demo.entiy.Bgd;
import com.example.demo.entiy.ReturnModel;
import com.example.demo.entiy.YCxmReturn;
import com.example.demo.utils.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

@Controller
@RequestMapping("/dw")
public class DWController {
    @Autowired
    GsServicesImpl gsServices;
    @Autowired
    TjServicesImpl tjServices;
    MyUtils myUtils=new MyUtils();
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String Index(int sess,Model model){
        String url="/dw/index";
        if (sess==1){
            url="/dw/index";
        }else {
            url="/dw/index-sj";
        }

        return  myUtils.renturnUrl(url,model) ;
    }
    @RequestMapping("/selpage")
    public String gscx(int sess,Model model){
        if (sess==1) {
            return myUtils.renturnUrl("/dw/dwhz",model);
        }else {
            return myUtils.renturnUrl("/dw/index-sj",model);
        }

    }
    @RequestMapping(value = "/sel",method = RequestMethod.POST)
     @ResponseBody
    public Object select(String start,String end){
        HttpSession session=index.session;
        Object data="";
        String status="true";
        String msg="";
        String username=session.getAttribute("username").toString();
        username=username.substring(0,username.indexOf("-"));
        List<Bgd> li=gsServices.selectAll(start,end,username);
        data=li;
        msg=start+"—"+end;
        return ReturnModel.getModel(msg,status,data);
    }

    @RequestMapping("/cxgr")
    public String selDw(int sess,Model model){
        String url = "/dw/cxgr";
        if (sess==2) {
            url = "/dw/cxgr-sj";
        }
        return  myUtils.renturnUrl(url,model) ;
    }
    @RequestMapping(value = "/grcx",method = RequestMethod.POST)
    @ResponseBody
    public Object selGSGR(String name,String idc){
        Object data="";
        String status="true";
        String msg="";
        HttpSession session=index.session;
        String username=myUtils.ReturnUsername(session);
        List<Bgd> li=gsServices.selByNameOrBhkCod(username,name,idc);
        data=li;
        if (name.equals("1")) msg="体检编码为:"+idc;
        if (idc.equals("0")) msg="姓名为:"+name;
        return  ReturnModel.getModel(msg,status,data);

    }

    @RequestMapping("/exit")
    public String exit(){
        HttpSession session=index.session;
        session.removeAttribute("username");

        return "/tj/index";
    }

    @RequestMapping(value = "/ycxm",method = RequestMethod.GET)
    public String xmyc(int sess,Model model){

        String url="/dw/ycxm";
        if (sess==2)  url="/dw/ycxm-sj";
        String urls=myUtils.renturnUrl(url,model);
        if (url.equals(urls)){
            model.addAttribute("li",gsServices.selectXm());
        }
        return  urls;
    }

    @RequestMapping(value = "/ycxmcx",method = RequestMethod.POST)
    @ResponseBody
    public Object xmyccx(String[] ycxm,String start,String end){
        String status="";
        String msg="";
        Object data="";
        HttpSession session=index.session;
        String username=myUtils.ReturnUsername(session);
        List<YCxmReturn> li=new LinkedList<>();
        status=start+"--"+end;
        for(String idx : ycxm){
            //Object obj=ycxm[i];
            YCxmReturn yc=new YCxmReturn();
            yc.setYcxm(gsServices.YCxmNam(idx));
            msg+=gsServices.YCxmNam(idx)+",";
            yc.setLi(gsServices.selYcxm(username,start,end,idx));
            if (yc.getLi()==null||yc.getLi().size()==0){
                continue;
            }else {
                li.add(yc);
            }
        }
        if (li.size()>0) {
            data = li;
        }else {
            status="false";
        }
        return ReturnModel.getModel(msg,status,data);
    }

    @RequestMapping("/tjwc")
    public String TjwcInfo(int sess,Model model){
        String url="/dw/wwctjcx";
        if (sess==2) url="/dw/wwctjcx-sj";
        String urls=myUtils.renturnUrl(url,model);
        if (url.equals(urls)){
            model.addAttribute("li",gsServices.selectXm());
        }
        return  urls;
    }

    @RequestMapping(value = "/wwctjcxs",method = RequestMethod.POST)
    @ResponseBody
    public Object TJwcCX(String start,String end){
        String msg="";
        String status="true";
        Object data="";
        HttpSession session=index.session;
        String username=myUtils.ReturnUsername(session);
        List<Bgd> li=gsServices.selWWc(username,start,end);
        if (li.size()>0) {
            for (int a = 0; a < li.size(); a++) {
                Bgd bgd = li.get(a);
                String wwcxm = gsServices.selWWC2(bgd.getIdx());
                if (wwcxm.equals("") || wwcxm == null) {
                    li.get(a).setWwcxm("无未完成项目");
                } else {
                    li.get(a).setWwcxm(wwcxm);
                }

            }
            data=li;
        }else {
            status="false";
            msg="未查到未完成项目信息";
        }
        return  ReturnModel.getModel(msg,status,data);
    }
}
